<?php
// +----------------------------------------------------------------------
// | 积分换礼栏目
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;
use Think\Controller;
class JifenController extends Base {

    protected  function _initialize(){
        parent::_initialize();
        //中间图片
        $this->midad=$this->getAdvert(5,1,0,1);
        //右侧广告
        $this->rightad=parent::getAdvert(5,3,0,1);
    }

    //首页显示
    public function index(){
        //列表
        $GiftsModel=M('Gifts');
        $where['status']=array('EQ',true);
        $count = $GiftsModel->where($where)->count();
        $page = $this->Pages($count, 4);
        $pageurl='?page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/jifen/'.$pageurl;

        $list=$GiftsModel
            ->where($where)
            ->field('id,title,image,needjf,dhnum,stocknum,requestnum,content')
            ->order(array('iftj DESC','sort DESC'))
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        foreach($list as $k=>$v){
            $list[$k]['rcount']=M('GiftsApply')->where(array('userid'=>$this->userid,'giftid'=>$v['id']))->count();
        }
        $this->assign("list", $list);
        $this->assign("Page", $page->show());

        //最新兑换情况
        $Model=new Model();
        $applylist=$Model
            ->table(array(
                    C('DB_PREFIX').'gifts'=>'G',
                    C('DB_PREFIX').'gifts_apply'=>'A',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('A.userid=U.userid and G.id=A.giftid and A.status=true')
            ->field('G.title,U.nickname,U.uavatar,U.openuid,U.userid,A.create_time,G.id')
            ->order('A.create_time DESC')
            ->limit(6)
            ->select();
        $this->assign("applylist", $applylist);

        $this->display();
    }

    //详情页面
    public function detail(){
        $giftid=I('get.id');
        if(empty($giftid)){
            $this->error('您访问的页面不存在!');
        }
        $data=M('Gifts')->where(array('status'=>true,'id'=>$giftid))->find();
        $this->rcount=M('GiftsApply')->where(array('userid'=>$this->userid,'giftid'=>$giftid))->count();
        $this->data=$data;

        $model=New Model();
        $dhlist=$model->table(array(
                C('DB_PREFIX').'gifts_apply'=>'A',
                C('DB_PREFIX').'user'=>'U',
            )
        )
            ->where('A.userid=U.userid and A.status=true and A.giftid='.$giftid)
            ->field('U.nickname,U.uavatar,U.openuid,U.userid,A.create_time')
            ->group('U.userid')
            ->order('A.create_time DESC')
            ->limit(6)
            ->select();
        $this->dhlist=$dhlist;

        $this->display();
    }

    //兑换操作
    public function apply(){
        $this->check(1);
        $get=I('get.');
        $get['userid']=$this->userid;
        $get['giftid']=$get['infoid'];
        unset($get['infoid']);
        $gifts=M('Gifts')->field('needjf,title')->where(array('id'=>$get['giftid']))->find();
        $needjf=$gifts['needjf'];
        if($this->userinfo['upoint']<$needjf){
            $this->error('<span>您的积分不足,无法兑换该礼品!</span>');
        }
        $count=M('GiftsApply')->where(array('userid'=>$get['userid'],'giftid'=>$get['giftid']))->count();
        if($count){
            $this->error('<span>请已经兑换过该礼品了,请不要重复兑换!</span>');
        }
        $get['status']=false;
        $get['create_time']=time();
        $result=M('GiftsApply')->add($get);
        if($result){
            $data['requestnum'] = array('exp','requestnum+1');
            $msg = "你在积分换礼中，申请了<a class=\"yf-list-type\" href=\"/jifen/detail/$get[giftid].html\">".$gifts['title']."</a>，扣除".$needjf."积分";//yf-0928
            service('Passport')->userDynamic($this->userid, $needjf, $msg, 2, 'gift', $get['giftid']);//yf-0925
            M('Gifts')->where(array('id'=>$get['giftid']))->save($data);
            $this->success('<span>您已成功兑换该礼品,请耐心等待我们联系!</span>');
        }else{
            $this->error('<span>兑换出错,请稍候再试!</span>');
        }
    }

}